﻿/**
 * @scope enchant.JGZ.Timer.prototype
 */
enchant.JGZ.Timer = enchant.Class.create(enchant.JGZ.Label, {
  /**
   * タイマーの作成・更新等を行う
   * @constructs
   * @param {Object}  opt {time: 初期タイム, text: 初期テキスト}
   */
  initialize : function(opt) {
    opt.sec       = opt.time;
    this._opt     = opt;
    opt.text      = this.manipulateText(opt.text, opt);
    this._obj     = enchant.JGZ.Label.call(this, opt);
    this._nTimer  = 0;
  },
  /**
   * タイマーアップデート
   */
  update : function() {
    var g         = enchant.Game.instance;
    var p         = parseInt(g.frame % g.fps);

    if (p === 0) {
      this._nTimer++; 
    }

    var scale     = (60 / parseInt(g.fps));
    var msec      = 60 - (Math.floor(g.frame % g.fps) * scale);
    var t         = this._opt.time - this._nTimer;
    var min       = parseInt(t / 60);
    var sec       = parseInt(t % 60);
    msec = ((min <= 0) && (sec <= 0)) ? 0 : msec;
    var time      = ("0" + min).slice(-2) + '.' + ("0" + sec).slice(-2) + '.' + ("0" + msec).slice(-2);
    if (this._opt.render !== undefined) {
      time = t; 
    }
    this.text     = this.manipulateText(time, this._opt);
    this._opt.sec = t;
  },
  /**
   * タイマーテキストを加工
   */
  manipulateText : function(text, opt) {
    if (opt.prefix !== undefined) {
      text = opt.prefix + text; 
    }
    if (opt.suffix !== undefined) {
      text = text + opt.suffix; 
    }
    return text;
  },

  sec : {
    get: function() {
      return this._opt.sec;      
    },
    set: function() {
    }
  },

  options : {
    get: function() {
      return this._opt;      
    },
    set: function() {
    }
  }
});
